// pages/sigin/sigin.js
var app=getApp();
import WxValidate from '../../utils/WxValidate.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgPath:app.globalData.imgPath,
    agree:true,
    buttonText: '短信验证码',
    disabled: false,
    tel:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initValidate();
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  inputtel:function(e){
    this.setData({tel:e.detail.value})
  },
  changeagree:function(e){
    this.setData({agree:e.detail.value})
  },
  sigin:function(e){
      let params=e.detail.value;
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false
    }
      if(this.data.agree){
        let data={
          phone:params.phone,
          password:params.password,
          referPhone: params.referPhone,
          code:params.code
        }
        wx.request({
          url: app.globalData.reqPath+'shop/Members',
          method:'POST',
          data:data,
          header:{
            'authorization': app.globalData.authorization
          },
          success:function({data}){
              if(data.code==200){
                wx.redirectTo({
                  url: '/pages/login/login',
                })
              }
          }
        })
        console.log(data)
      }
  },
  //获取验证码
  getCode: function () {
    let phone = this.data.tel;
    let self = this;
    if (phone.match(/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/)) {
      wx.request({
        url: app.globalData.reqPath + 'shop/MembersSms',
        header: {
          'authorization': app.globalData.authorization
        },
        data: {
          phone: self.data.tel
        },
        method: "POST",
        success: function ({ data, header }) {
          // console.log(res.data);
          app.globalData.authorization = header.authorization
          let num = 61;
          var timer = setInterval(function () {
            num--;
            if (num <= 0) {
              clearInterval(timer);
              self.setData({
                buttonText: '重新发送',
                disabled: false
              })
            } else {
              self.setData({
                buttonText: num + "s后重新发送",
                disabled: true
              })
            }
          }, 1000)
        }

      })
    } else {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
    }
  },
  //表单验证
  initValidate() {
    const rules = {
      referPhone: {
        required: false,
        tel: true
      },
      phone: {
        required: true,
        tel: true
      },
      password:{
        required: true,
      },
      code:{
        required: true,
      }
    }
    const messages = {
      referPhone: {
        tel: "请输入正确的手机号码"
      },
      phone: {
        required: "请输入手机号码",
        tel: "请输入正确的手机号码"
      },
      password: {
        required: "请输入密码码",
      },
      code: {
        required: "请输入验证码",
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  showModal: function (error) {
    wx.showToast({
      title: error.msg,
      icon:'none'
    })
  }
})